package com.androidhuman.example.mapsv2example;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MakeActivity extends FragmentActivity {
	int N = 1;
	String text;
	String detail;
	private static final int CAPTURE_IMAGE_CAPTURE_CODE = 0;
	private static final int REQUEST_GALLERY_IMAGE_CODE = 1;

	Intent i;
	ImageView pic;
	ImageView pic2;

	Bitmap resized = null;

	View make_camera;
	View make_gallery;

	GPSTracker gps;

	TextView rAddressView;
	EditText rTitleView;
	EditText rDetailAddressView;
	EditText rTextView;
	TextView rTypeView;

	SharedPreferences userPrefs;
	SharedPreferences.Editor userPrefsEditor;

	String make_title;
	String make_address;
	String make_detail_address;
	String make_text;
	int make_icon;

	View make_dis1;
	View make_dis2;
	View make_dis3;

	String imagepath;
	String Photoname;

	int imagecheck = 0;

	int userinfo;

	int if_click = 0;
	double latitude;
	double longitude;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_make);

		pic = (ImageView) findViewById(R.id.pic1);
		pic2 = (ImageView) findViewById(R.id.pic2);

		userPrefs = getSharedPreferences("USER", MODE_PRIVATE);
		userPrefsEditor = userPrefs.edit();

		userinfo = userPrefs.getInt("myid", 0);

		resized = null;

		Intent intent = getIntent();
		if_click = intent.getIntExtra("if_click", 0);
		
		Intent intent1 = getIntent();
		latitude=intent1.getFloatExtra("latitude", 0);
		
		Intent intent2 = getIntent();
		longitude=intent2.getFloatExtra("longitude", 0);

		make_camera = (View) findViewById(R.id.make_camera);
		make_gallery = (View) findViewById(R.id.make_gallery);

		rAddressView = (TextView) findViewById(R.id.make_address);
		rAddressView.setText("ㅎㄷㄷ");

		rTitleView = (EditText) findViewById(R.id.make_title);
		rTextView = (EditText) findViewById(R.id.make_text);
		rDetailAddressView = (EditText) findViewById(R.id.make_detail_address);

		make_dis1 = (View) findViewById(R.id.make_dis1);
		make_dis2 = (View) findViewById(R.id.make_dis2);
		make_dis3 = (View) findViewById(R.id.make_dis3);

		rTypeView = (TextView) findViewById(R.id.make_what);

		if (if_click == 1) {
			rTypeView.setText("이벤트 추가하기");

			make_dis3.setVisibility(View.GONE);

		} else if (if_click == 2) {

			rTypeView.setText("현수막 달기");
			make_dis1.setVisibility(View.GONE);
			make_dis2.setVisibility(View.GONE);
			make_dis3.setVisibility(View.GONE);
			rTitleView.setHint("내용");
		}else if(if_click ==0){

			rTypeView.setText("0이 넘어옴");
		}

		Button makemake_close = (Button) findViewById(R.id.makebanner_close);

		makemake_close.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				make_title=rTitleView.getText().toString();

				if(if_click == 1){
					make_detail_address=rDetailAddressView.getText().toString();	//dis1
					make_text=rTextView.getText().toString();						//dis2
					event_insert();
				}
				else if(if_click == 2)
				{
					banner_insert();
				}
				MakeActivity.this.finish();

				// ipm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
				// 0);
			}
		});

		make_camera.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {

				Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(i, CAPTURE_IMAGE_CAPTURE_CODE);

			}
		});

		make_gallery.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {

				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(
						Intent.createChooser(intent, "Complete action using"),
						1);

			}
		});

	}

	public void event_insert()
	{
		GPSTracker gps=new GPSTracker(this);
		String str = HttpRequest.get(
				"http://dev.ts.hs.kr/trend/reporters_make_events.php", true,
				"latitude", latitude, "longitude",
				longitude, "title", make_title,
				"text", make_text,
				"user_id", userinfo,
				"address", gps.getAddress(latitude, longitude),
				"detail_address", make_detail_address
				).body();
	}

	public void banner_insert()
	{
		String str = HttpRequest.get(
				"http://dev.ts.hs.kr/trend/reporters_make_banners.php", true,
				"latitude", latitude, "longitude",
				longitude, "text", make_title,
				"user_id", userinfo
				).body();
	}

	public void article_insert(int event_id) {

		//		make_title = rTitleView.getText().toString();
		//		make_text = rTextView.getText().toString();
		//		make_detail_address = rDetailAddressView.getText().toString();
		//
		//		String strArticle_id = HttpRequest.get(
		//				"http://dev.ts.hs.kr/trend/reporters_make_events.php", true,
		//				"latitude", GPSTracker.latitude, "longitude",
		//				GPSTracker.longitude, "title", make_title, "text", make_text,
		//				"event_id", event_id, "user_id", userinfo, "address", "ㅎㄷㄷ",// make_address,
		//				"detail_address", make_detail_address).body();
		//		if (strArticle_id != null && strArticle_id != "") {
		//			Toast toast1 = Toast.makeText(getBaseContext(), "어머! 글이 올라갔어요!!"
		//					+ "\n글의 ID는 " + strArticle_id + "입니다.", Toast.LENGTH_LONG);
		//			toast1.show();
		//		}
		//
		//		String stringArticle_id = "";
		//		String temp = "";
		//		for (int i = 0; i < strArticle_id.length(); i++) {
		//			temp = strArticle_id.substring(i, i + 1);
		//			if (temp.charAt(0) > 47 && temp.charAt(0) < 58) {
		//				stringArticle_id += temp;
		//			}
		//		}
		//
		//		int article_id = Integer.parseInt(stringArticle_id);
		//
		//		Photoname = "1_" + stringArticle_id + "_"; // 프로필사진:0 기사:1 이벤:2
		//		if (imagecheck == 1) {
		//
		//			File imageFile = new File(imagepath);
		//			BitmapFactory.Options options = new BitmapFactory.Options();
		//			options.inSampleSize = 1;
		//			Bitmap bitmap = BitmapFactory.decodeFile(imagepath, options);
		//			String fileName = Uri.parse(imagepath).getLastPathSegment();
		//
		//			imagepath = getBaseContext().getCacheDir() + fileName;
		//			SaveBitmapToFileCache(bitmap, imagepath, 1);
		//			long L = imageFile.length();
		//			for (; (L / (2 * 2)) > 2097152l;) {
		//				options = new BitmapFactory.Options();
		//				options.inSampleSize = 2;
		//				bitmap = BitmapFactory.decodeFile(imagepath, options);
		//				SaveBitmapToFileCache(bitmap, imagepath, 1);
		//				imageFile = new File(imagepath);
		//				L = imageFile.length();
		//			}
		//
		//			RequestParams params = new RequestParams();
		//			params.put("title", Photoname);
		//
		//			try {
		//				params.put("image", imageFile);
		//			} catch (Exception e) {
		//				// Log.i("1", "oh no");
		//			}
		//
		//			AsyncHttpClient client = new AsyncHttpClient();
		//			client.post("http://dev.ts.hs.kr/trend/phototemp.php", params,
		//					new AsyncHttpResponseHandler() {
		//						public void onSuccess(String response) {
		//							Log.w("async", response);
		//							setResult(RESULT_OK, new Intent());
		//
		//						}
		//					});
		//
		//		}
		//		String picUpload = HttpRequest.get(
		//				"http://dev.ts.hs.kr/trend/reporters_photo_linker.php", true,
		//				"id", article_id, "type", 1).body();
		//		if (picUpload.compareTo("SUCCESS") == 0) {
		//			Toast toast1 = Toast.makeText(getBaseContext(), "어머! 글이 올라갔어요!!"
		//					+ "\n글의 ID는 " + strArticle_id + "입니다.", Toast.LENGTH_LONG);
		//			toast1.show();
		//		}
		//
		//		this.finish();

	}

	public void article_insert() {
		article_insert(-1);
	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		return true;
	}

	private class CData {
		private String Title;
		private String Text;
		private int Img;
		private int Photo;

		public CData(Context context, int mPhoto, String mTitle, String mText,
				int mImg) {
			Photo = mPhoto;
			Title = mTitle;
			Text = mText;
			Img = mImg;
		}

		public CData(Context context, int mPhoto, String mTitle, String mText) {
			Photo = mPhoto;
			Title = mTitle;
			Text = mText;
			Img = 123456;

		}

		public String getTitle() {
			return Title;
		}

		public String getText() {
			return Text;
		}

		public int getImg() {
			return Img;
		}

		public int getPhoto() {
			return Photo;
		}
	}





}